package com.huangd.platform.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangd.platform.modules.sys.dao.TblRoleDao;
import com.huangd.platform.modules.sys.entity.TblRoleEntity;
import com.huangd.platform.common.utils.PageUtils;
import com.huangd.platform.common.utils.Query;
import com.huangd.platform.modules.sys.service.SysRoleMenuService;
import com.huangd.platform.modules.sys.service.TblRoleService;
import com.huangd.platform.modules.sys.service.TblUserRoleService;
import com.huangd.platform.modules.sys.service.TblUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service("tblRoleService")
public class TblRoleServiceImpl extends ServiceImpl<TblRoleDao, TblRoleEntity> implements TblRoleService {
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private TblUserService sysUserService;
    @Resource
    private TblUserRoleService sysUserRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String)params.get("roleName");

        IPage<TblRoleEntity> page = this.page(
                new Query<TblRoleEntity>().getPage(params),
                new QueryWrapper<TblRoleEntity>()
                        .like(StringUtils.isNotBlank(roleName),"role_name", roleName)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(TblRoleEntity role) {
        this.save(role);

        //检查权限是否越权

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TblRoleEntity role) {
        this.updateById(role);

        //检查权限是否越权

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
    public List<Long> queryRoleIdList(Long createUserId) {
        return baseMapper.queryRoleIdList(createUserId);
    }

    @Override
    public List<TblRoleEntity> getListByMap(Map map) {
        List<TblRoleEntity> list = new ArrayList<>();
        if (map.size() == 0) {
            list = this.listByMap(map);
        } else {
            list = baseMapper.getRoleByUid(new Long((Integer) map.get("user_id")));
        }


        return list;
    }

}